package com.ym.b2b.product.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.commons.log.CusLog;
import com.ym.b2b.product.dao.ProductDao;
import com.ym.b2b.product.service.ProductService;
import com.ym.b2b.product.sqlbean.EmBProduct;
import com.ym.b2b.product.sqlbean.EmBProductIntroduction;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;
import com.zc.export.ExcelUtil;
import com.zc.security.acl.domain.Users;

@Controller
@RequestMapping("/product")
public class ManagerProductController extends BaseController {

    @Autowired
    private ProductService productService;
    @Autowired
    private ProductDao     productDao;

    @RequestMapping("searchPageProduct.do")
    @ResponseBody
    public Result searchPageProduct(EmBProduct emBProduct, Page<EmBProduct> page, HttpServletRequest req) {
        Result result = new Result();
        try {
            result = productService.searchPageProduct(emBProduct, page);
            return result;
        } catch (Exception e) {
        	e.printStackTrace();
        	
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "searchPageProductExcel.do")
    @ResponseBody
    public Result searchPageProductExcel(EmBProduct emBProduct, Page<EmBProduct> page, String loadType) {
        Result ret = new Result();
        try {
            if (loadType == null || loadType.equals("")) {
                ret.setCode(Result.CODE_100);
                ret.setResult("loadType 参数 '数量'  不能为空!");
                return ret;
            }
            page.setPageable("true");
            page.setPageSize(Integer.parseInt(loadType));
            page.setStartRow(1);
            ret = productService.searchPageProduct(emBProduct, page);
            ExcelUtil<EmBProduct> excelUtil = new ExcelUtil<EmBProduct>();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("model", ret.getRows());
            ret = excelUtil.createExcel("productListExcel", map);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;
    }

    @RequestMapping("searchPageProductSimple.do")
    @ResponseBody
    public Result searchPageProductSimple(EmBProduct emBProduct, Page<EmBProduct> page, HttpServletRequest req) {
        Result result = new Result();
        try {
            result = productService.searchPageProduct(emBProduct, page);
            return result;
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    /**
     * @Title: showProductDetail
     * @Description: 商品 详情
     */

    @RequestMapping("searchAllProductDetail.do")
    @ResponseBody
    public Result searchAllProductDetail(EmBProduct emBProduct, HttpServletRequest req) {
    	Users user = (Users) req.getSession().getAttribute("USER_INFO");
        CusLog.writeLog(user.getUserName() + " 查询商品详情:" + emBProduct.getProductCode(), "USER", 2);
        Result result = new Result();
        try {
            result = productService.searchAllProductDetail(emBProduct);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    /**
     * @Title: saveOrUpdateProductIntrduction
     * @Description: 保存或修改 商品说明书
     */
    @RequestMapping("saveOrUpdateProductIntrduction.do")
    @ResponseBody
    public Result saveOrUpdateProductIntrduction(EmBProductIntroduction emBProductIntroduction, HttpServletRequest req) {
    	Users user = (Users) req.getSession().getAttribute("USER_INFO");
        CusLog.writeLog(user.getUserName() + " 保存或修改 商品说明书:" + emBProductIntroduction.getProductCode(), "USER", 2);
        Result result = new Result();
        try {
            productService.saveOrUpdateProductIntrduction(emBProductIntroduction);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    /**
     * @Title: saveOrUpdateProductIntrduction
     * @Description: 删除商品 软删除
     */
    @RequestMapping("deleteEmBProduct.do")
    @ResponseBody
    public Result deleteEmBProduct(EmBProduct emBProduct, HttpServletRequest req) {
    	Users user = (Users) req.getSession().getAttribute("USER_INFO");
        CusLog.writeLog(user.getUserName() + " 删除商品 软删除:" + emBProduct.getProductCode(), "USER", 2);
        Result result = new Result();
        try {
            result = productService.deleteBatchEmBProduct(emBProduct);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    /**
     * @Title: saveOrUpdateProductIntrduction
     * @Description: 保存或修改 商品,,根据productId
     */
    @RequestMapping("saveOrUpdateEmBProduct.do")
    @ResponseBody
    public Result saveOrUpdateEmBProduct(EmBProduct emBProduct, HttpServletRequest req) {
    	Users user = (Users) req.getSession().getAttribute("USER_INFO");
        CusLog.writeLog(user.getUserName() + " 保存或修改 商品:" + emBProduct.getProductCode(), "USER", 2);
        Result result = new Result();
        try {
            result = productService.saveOrUpdateEmBProduct(emBProduct);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    @RequestMapping("updateEmBProduct.do")
    @ResponseBody
    public Result updateEmBProduct(EmBProduct emBProduct, HttpServletRequest req) {
    	Users user = (Users) req.getSession().getAttribute("USER_INFO");
        CusLog.writeLog(user.getUserName() + " 修改或审核商品:" + emBProduct.getProductCode(), "USER", 2);
        Result result = new Result();
        try {
            productDao.updateEmBProduct(emBProduct);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    @RequestMapping("batchUpdateEmBProduct.do")
    @ResponseBody
    public Result batchUpdateEmBProduct(EmBProduct emBProduct, HttpServletRequest req) {
    	Users user = (Users) req.getSession().getAttribute("USER_INFO");
        CusLog.writeLog(user.getUserName() + " 批量修改 商品", "USER", 2);
        Result result = new Result();
        try {
            result = productService.batchUpdateEmBProduct(emBProduct);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    /**
     * @Title: showEmBProductIntroductionDetail
     * @Description: 说明书详情
     */
    @RequestMapping("showEmBProductIntroductionDetail.do")
    @ResponseBody
    public Result showEmBProductIntroductionDetail(String productCode, HttpServletRequest req) {
    	Users user = (Users) req.getSession().getAttribute("USER_INFO");
        CusLog.writeLog(user.getUserName() + " 查看说明书详情", "USER", 2);
        Result result = new Result();
        try {
            result.putData("detail", productService.showEmBProductIntroductionDetail(productCode));
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

}
